//
//  ZLTextView.m
//  ZLTextView
//
//  Created by 科大物联 on 2023/6/26.
//

#import "ZLTextView.h"
#import <Masonry/Masonry.h>
@implementation ZLTextView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.textView];
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
        UIView *masView = [self getContainerView];
        if (masView) {
            [self addSubview:self.placeholderLabel];
            [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(masView);
                make.left.mas_equalTo(5);
                make.right.mas_equalTo(-5);
            }];
        }
        [self addSubview:self.numberLabel];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-10);
            make.bottom.equalTo(self).offset(-10);
        }];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textChange:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:self.textView];
    }
    return self;
}

- (void)textChange:(NSNotification *)notification {
    UITextView *textView = (UITextView *)[notification object];
    if (textView != self.textView) {
        return;
    }
    self.placeholderLabel.hidden = textView.text.length > 0;
    if (self.maxLength <= 0) {
        return;
    }
    if (self.textView.text.length > self.maxLength) {
        self.textView.text = [self.textView.text substringToIndex:self.maxLength];
        if ([self.zlDelegate respondsToSelector:@selector(textViewTextLengthDidMax)]) {
            [self.zlDelegate textViewTextLengthDidMax];
        }
    }
    self.numberLabel.text = [NSString stringWithFormat:@"%ld/%ld", textView.text.length, (long)self.maxLength];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - setter

- (void)setZlDelegate:(id)zlDelegate {
    _zlDelegate = zlDelegate;
    self.textView.delegate = zlDelegate;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    self.placeholderLabel.text = placeholder;
}

- (void)setFont:(UIFont *)font {
    self.textView.font = font;
    self.placeholderLabel.font = font;
    self.numberLabel.font = font;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    self.placeholderLabel.textColor = placeholderColor;
}

- (void)setNumberColor:(UIColor *)numberColor {
    _numberColor = numberColor;
    self.numberLabel.textColor = numberColor;
}

- (void)setMaxLength:(NSInteger)maxLength {
    _maxLength = maxLength;
    self.numberLabel.hidden = maxLength <= 0;
    self.numberLabel.text = [NSString stringWithFormat:@"0/%ld", (long)maxLength];
}

- (UIView *)getContainerView {
    for (UIView *subView in self.textView.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"_UITextContainerView")]) {
            return subView;
        }
    }
    return nil;
}

#pragma mark - init

- (UILabel *)placeholderLabel {
    if (!_placeholderLabel) {
        _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _placeholderLabel.textColor = [UIColor grayColor];
        _placeholderLabel.font = self.font;
    }
    return _placeholderLabel;
}

- (UILabel *)numberLabel {
    if (!_numberLabel) {
        _numberLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _numberLabel.textColor = [UIColor grayColor];
        _numberLabel.font = self.font;
    }
    return _numberLabel;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:CGRectZero];
    }
    return _textView;
}

@end
